package cn.djun.news.controller;

import cn.djun.news.entity.Comment;
import cn.djun.news.service.CommentService;
import cn.djun.news.util.BaseResponse;
import cn.djun.news.util.Constant;
import cn.djun.news.util.Paginating;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



/**
 * @author djun
 * @classname CommentController
 * @description
 * @date 2020/12/7 15:41
 * @email 3170495286@qq.com
 */
@Slf4j
@RestController
@RequestMapping("/comment")
@Api(tags = "评论新闻API")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 添加评论
     *
     * @param
     * @param
     * @return
     */
    @PutMapping("/add")
    @ApiOperation(value = "添加评论", notes = "添加评论", httpMethod = "PUT")
    public ResponseEntity<BaseResponse> addComment(@RequestBody Comment comment) {
        try {
            //调用service层方法
            int flag = commentService.addComment(comment);
            log.info("-----flag-----", flag);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.ADD_NEWS_SUCCESS, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.ADD_NEWS_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 根据新闻id查询评论数量
     * @param id
     * @return
     */
    @GetMapping(value = "/count/{id}")
    @ApiOperation(value = "根据新闻id查询评论数量", notes = "根据新闻id查询评论数量", httpMethod = "POST")
    public ResponseEntity<BaseResponse<Comment>> getcommentCount(@PathVariable("id") int id) {
        try {
            int comment = commentService.getCommentCountbynewid(id);
            if (comment == 0) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.NEWS_NO_COMMENT, comment);
            }
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, comment);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
    /**
     * 根据新闻id查询评论列表
     * 根据用户id查看评论列表
     * @param comment
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询评论列表", notes = "* 根据新闻id查询评论列表\n" +
            "* 根据用户id查看评论列表", httpMethod = "GET")
    public ResponseEntity<BaseResponse<Paginating<Comment>>> getcommentbynewsid(Comment comment) {
        try {
            Paginating<Comment> paginating = commentService.getcommentbynewsid(comment);
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, paginating);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
    /**
     * 根据id删除评论
     * @param comment
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "删除评论", notes = "根据id删除评论", httpMethod = "DELETE")
    public ResponseEntity<BaseResponse> delcommentbyid(Comment comment) {
        try {
            int flag = commentService.delcommentbyid(comment.getId());
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE,Constant.COMMENT_DELETE_SUCCESS, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.COMMENT_DELETE_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
}
